package blog.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import blog.entity.Notice;
import blog.service.NoticeService;

@Controller
@RequestMapping("notice")
public class NoticeController {
	
	@Autowired
	private NoticeService noticeService;
	/**
	 * 去公告主界面
	 * @param map
	 * @return
	 */
	@RequestMapping("")
	public String noticeList(ModelMap map) {
		List<Notice> findAllNotice = noticeService.findAllNotice();
		map.put("noticeList", findAllNotice);
		return "notice/notice_list";
	}
	/**
	 * 去添加公告界面
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String goAddNotice() {
		return "notice/notice_add";
	}
	/**
	 * 添加公告
	 * @param notice
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String addNotice(Notice notice) {
		notice.setNoticeCreateTime(new Date());
		notice.setNoticeStatus(1);
		notice.setNoticeOrder(1);
		noticeService.addNotice(notice);
		return "forward:/notice";
	}
	@RequestMapping(value="/edit/{noticeId}",method=RequestMethod.GET)
	public String goEdit(@PathVariable("noticeId")Integer noticeId,ModelMap map) {
		Notice notice = noticeService.getNoticeById(noticeId);
		map.put("notice",notice);
		return "notice/notice_edit";
	}
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public String edit(Notice notice) {
		noticeService.updateNotice(notice);
		notice.setNoticeUpdateTime(new Date());
		return "forward:/notice";
	}
}
